package main

import (
	"config"
	"devserv"
	"flag"
	"fmt"
	"github.com/client9/reopen"
	"log"
	"os"
	"os/signal"
	"runtime"
	"sentinel"
	"strconv"
	"syscall"
)

var (
	cfgFilepath = ""
)

func init() {
	flag.StringVar(&cfgFilepath, "c", "", "specify the proto file path name")
}

func main() {
	runtime.GOMAXPROCS(1)

	flag.Parse()
	if cfgFilepath != "" {
		r := config.ParseConfigFile(cfgFilepath)
		if r != true {
			log.Fatal("can't parse configure file:", cfgFilepath)
		}
	}

	log.Println("try to start wxserver...")

	if config.Daemon == "yes" && config.LogFile != "" {
		f, err := reopen.NewFileWriter(config.LogFile)
		if err != nil {
			log.Fatalf("Unable to set output log: %s", err)
		}
		defer f.Close()
		log.SetOutput(f)

		sighup := make(chan os.Signal, 1)
		signal.Notify(sighup, syscall.SIGHUP)
		go func() {
			for {
				<-sighup
				fmt.Printf("Got a sighup\n")
				f.Reopen()
			}
		}()
	}

	//userserv.SetupFileServer()
	sentinel.StartSentinel()

	devserv.CreateHttpServer()

	go devserv.SetKeyExpire()

	go sentinel.MonitorRedis()

	//userserv.CreateHttpServer()
	//go webserver.StartWebServer()

	log.Println("start wxserver ok!")

	if config.Daemon == "yes" {
		waitForSignal()
	} else {
		waitInput()
	}
	return
}

func waitInput() {
	var cmd string
	var option string
	for {
		_, err := fmt.Scanf("%s %s\n", &cmd, &option)
		if err != nil {
			//log.Println("Scanf err:", err)
			continue
		}

		switch cmd {
		case "exit", "quit":
			log.Println("exit by user")
			return
		case "gr":
			log.Println("current goroutine count:", runtime.NumGoroutine())
			break
		case "power":
			// i, _ := strconv.Atoi(option)
			devserv.TestOpenOrBreak(option)
			break
		case "speedLeve":
			i, _ := strconv.Atoi(option)
			devserv.TestSetSpeedLeve(i)
			break
			/*		case "moon":
					// i, _ := strconv.Atoi(option)
					devserv.TestMoon(option)
					break*/

		case "runtime":
			devserv.GetRuntime(option)
			break
		case "ion":
			// i, _ := strconv.Atoi(option)
			devserv.TestIon(option)
			break
		case "auto":
			// i, _ := strconv.Atoi(option)
			devserv.TestAuto(option)
			break
		case "update":
			// i, _ := strconv.Atoi(option)
			devserv.TestUpdate()
			break
		case "speed":
			i, _ := strconv.Atoi(option)
			devserv.TestSetSpeed(i)
			break
		case "selfClean":
			i, _ := strconv.Atoi(option)
			devserv.TestSetSelfClean(i)
			break
		case "childLock":
			i, _ := strconv.Atoi(option)
			devserv.TestSetChildLock(i)
			break
		case "autoSet":
			i, _ := strconv.Atoi(option)
			devserv.TestAutoSet(i)
			break
		case "timerc":
			log.Println("timer")
			i, _ := strconv.Atoi(option)
			devserv.TestTimerC(i)
		case "timero":
			log.Println("timer")
			i, _ := strconv.Atoi(option)
			devserv.TestTimerO(i)
		case "schedulec":
			log.Println("schedule")
			i, _ := strconv.Atoi(option)
			devserv.TestSchedulerC(i)
		case "scheduleo":
			log.Println("schedule")
			i, _ := strconv.Atoi(option)
			devserv.TestSchedulerO(i)
		case "delSchedule":
			log.Println("delSchedule")
			i, _ := strconv.Atoi(option)
			devserv.TestDelSchedule(i)
		default:
			break
		}
	}
}

func waitForSignal() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)

	// Block until a signal is received.
	s := <-c
	fmt.Println("Got signal:", s)
}
